package ui.magic.magiclibs.container.Drawable;

import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

/**
 * Created by liushiyin on 2017/10/25.
 */

public class PowerDrawable extends Drawable {

    private Paint paint;
    private Bitmap bitmap=null;
    private int roundWidthX;
    private int roundWidthY;
    private RectF rectF;
    private boolean isCircle;

    public PowerDrawable(){
        rectF=new RectF();
        paint=new Paint();
        paint.setAntiAlias(true);
        roundWidthX=10;
        roundWidthY=10;
        paint.setColor(0xFFC1C1C1);
    }

    public PowerDrawable(Bitmap bitmap){
        rectF=new RectF();
        this.bitmap=bitmap;
        paint=new Paint();
        paint.setAntiAlias(true);
        roundWidthX=10;
        roundWidthY=10;
    }

    @Override
    public void setBounds(int left, int top, int right, int bottom) {
        super.setBounds(left, top, right, bottom);
        if(rectF.left==left&&rectF.right==right&&rectF.top==top&&rectF.bottom==bottom){
                return;
        }
        rectF.left=left;
        rectF.right=right;
        rectF.bottom=bottom;
        rectF.top=top;
        if(bitmap!=null){
            BitmapShader shader=new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            Matrix matrix=new Matrix();
            float scaleX = (rectF.right-rectF.left) / bitmap.getWidth();
            float scaleY = (rectF.bottom-rectF.top) / bitmap.getHeight();
            float scale = Math.max(scaleX, scaleY);
            matrix.setScale(scale, scale);
            shader.setLocalMatrix(matrix);
            paint.setShader(shader);
        }
    }

    @Override
    public void draw(@NonNull Canvas canvas) {
        if(isCircle){
            canvas.drawRoundRect(rectF,(rectF.right-rectF.left)/2,(rectF.bottom-rectF.top)/2,paint);
        }else{
            canvas.drawRoundRect(rectF,roundWidthX,roundWidthY,paint);
        }
    }

    @Override
    public void setAlpha(@IntRange(from = 0, to = 255) int alpha) {
        paint.setAlpha(alpha);
    }

    @Override
    public int getIntrinsicWidth() {
        if(bitmap!=null){
            return bitmap.getWidth();
        }else{
            return super.getIntrinsicWidth();
        }
    }

    @Override
    public int getIntrinsicHeight() {
        if(bitmap!=null){
            return bitmap.getHeight();
        }else{
            return super.getIntrinsicHeight();
        }
    }

    @Override
    public void setColorFilter(@Nullable ColorFilter colorFilter) {
        paint.setColorFilter(colorFilter);
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }

    public void setColor(int color){
        paint.setColor(color);
    }

    public void setRoundWidth(int widthX,int widthY){
        roundWidthX=widthX;
        roundWidthY=widthY;
    }

    public void setCircle(boolean b){
        isCircle=b;
    }

    public void setBitmap(Bitmap bitmap){
        this.bitmap=bitmap;
        BitmapShader shader=new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        Matrix matrix=new Matrix();
        float scaleX = (rectF.right-rectF.left) / bitmap.getWidth();
        float scaleY = (rectF.bottom-rectF.top) / bitmap.getHeight();
        float scale = Math.max(scaleX, scaleY);
        matrix.setScale(scale, scale);
        shader.setLocalMatrix(matrix);
        paint.setShader(shader);
    }
}
